package logic

import (
	"class/models"
	"context"

	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 收藏列表
func (l *CollectListLogic) CollectList(in *classrpc.CollectListRequest) (*classrpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line

	var collect []models.CollectData
	models.DB.Debug().Model(models.Collection{}).
		Select("collect .* title ,name ").
		Joins("users on users.id = collections.user_id").
		Joins("courses on courses.id = collections.course_id").
		Find(&collect)

	var collects []*classrpc.CollectList
	for _, collection := range collect {
		list := classrpc.CollectList{
			Title:    collection.Title,
			Name:     collection.Name,
			CourseId: int64(collection.CourseId),
			UserId:   int64(collection.UserId),
		}
		collects = append(collects, &list)
	}

	return &classrpc.CollectListResponse{
		List: collects,
	}, nil
}
